package org.jeecg.modules.demo.crm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.demo.crm.entity.*;
import org.jeecg.modules.demo.crm.mapper.*;
import org.jeecg.modules.demo.crm.service.ICProjectPriceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description: 项目数据看板
 * @Author: jeecg-boot
 * @Date:   2025-08-19
 * @Version: V1.0
 */
@Service
public class CProjectPriceServiceImpl extends ServiceImpl<CProjectPriceMapper, CProjectPrice> implements ICProjectPriceService {

    @Autowired
    CProjectHeadQuotesMapper cProjectHeadQuotesMapper;

    @Autowired
    CProjectApplicationMapper cProjectApplicationMapper;
    @Autowired
    CSubProjectMapper cSubProjectMapper;
    @Autowired
    CContractMapper cContractMapper;
    @Autowired
    CInvoiceApplyMapper cInvoiceApplyMapper;

    @Autowired
    CPaymentRecordMapper cPaymentRecordMapper;

    @Autowired
    CInvoiceApplySplitMapper cInvoiceApplySplitMapper;


    @Override
    public IPage<CProjectPrice> queryPageList(CProjectPrice cProjectPrice,Integer pageNo,Integer pageSize, HttpServletRequest req) {
        CProjectHeadQuotes cProjectHeadQuotes = new CProjectHeadQuotes();
        cProjectHeadQuotes.setProjectId(cProjectPrice.getProjectId());
        cProjectHeadQuotes.setPaymentStatus(cProjectPrice.getPaymentStatus());
        cProjectHeadQuotes.setTitle(cProjectPrice.getProjectName());


        QueryWrapper<CProjectHeadQuotes> queryWrapper = QueryGenerator.initQueryWrapper(cProjectHeadQuotes, req.getParameterMap());
        queryWrapper.orderByDesc("create_time");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
        Page<CProjectHeadQuotes> page = new Page<CProjectHeadQuotes>(pageNo, pageSize);
        IPage<CProjectPrice> pageProjectPrice = new Page<>(pageNo, pageSize);

        IPage<CProjectHeadQuotes> pageList  = cProjectHeadQuotesMapper.selectPage(page,queryWrapper);
        List<CProjectPrice> data =  pageList.getRecords().stream().map(quotes->{
            CProjectPrice projectPrice = new CProjectPrice();
            projectPrice.setId(quotes.getId());
            projectPrice.setProjectId(quotes.getProjectId());
            projectPrice.setPaymentStatus(quotes.getPaymentStatus());
            projectPrice.setYears(quotes.getQuotesTime()==null?"":sdf.format(quotes.getQuotesTime()));
            projectPrice.setProjectHeadQuotesId(quotes.getId());

            //查询库
            CProjectApplication application = cProjectApplicationMapper.selectById(quotes.getProjectId());
            if(application != null){
                projectPrice.setLibraryId(application.getLibraryId());
            }


            //查询子项目--获取子项目编号
            CSubProject cSubProject = cSubProjectMapper.selectById(quotes.getSubProjectId());
            if(cSubProject != null){
                projectPrice.setProjectCode(cSubProject.getProjectNumber());
            }

            //查询合同
            CContract cContract = cContractMapper.selectById(quotes.getContractId());
            if(cContract != null){
                projectPrice.setContractNo(cContract.getContractNo());
                projectPrice.setProjectName(cContract.getName());
                //合同金额(不含税)
                projectPrice.setContractAmount(cContract.getContractAmount());
                //含税价
                projectPrice.setTaxincludingPrice(cContract.getTaxIncludingPrice());

                //未收款金额 = 含税价 - 收款记录金额
                BigDecimal receivingPayments = cInvoiceApplySplitMapper.findbyInvoice(cContract.getInvoiceApplyId());
                BigDecimal notReceivingPayments = projectPrice.getTaxincludingPrice().subtract(receivingPayments);
                projectPrice.setUncollectedPayment(notReceivingPayments);

                //未开票金额 = 含税价 - 已开票金额
                List<String> invoiceApplyIdList = new ArrayList<>(Arrays.asList(cContract.getInvoiceApplyId().split(",")));
                //开票金额
                BigDecimal totalAmountWithTaxByIds = cInvoiceApplyMapper.getTotalAmountWithTaxByIds(invoiceApplyIdList);
                BigDecimal uninvoicedAmount = projectPrice.getTaxincludingPrice().subtract(totalAmountWithTaxByIds);
                projectPrice.setUninvoicedAmount(uninvoicedAmount);

                //收款日期
                CPaymentRecord paymentRecord = cInvoiceApplySplitMapper.getInvoiceTime(cContract.getInvoiceApplyId());
                if(paymentRecord != null){
                    projectPrice.setPaymentRecord(paymentRecord.getPaymentDate());
                }


                //质保金
                projectPrice.setDeposit(cContract.getWarrantyPrice());

                //发票信息
                projectPrice.setInvoiceInformation(null);
            }
            return projectPrice;
        }).collect(Collectors.toList());
        pageProjectPrice.setRecords(data);
        pageProjectPrice.setTotal(pageList.getTotal());
        pageProjectPrice.setCurrent(pageList.getCurrent());
        pageProjectPrice.setSize(pageList.getSize());

        return pageProjectPrice;
    }

    @Override
    public void updateStatus(CProjectPrice cProjectPrice) {
        CProjectHeadQuotes projectHeadQuotes = cProjectHeadQuotesMapper.selectById(cProjectPrice.getId());
        if(projectHeadQuotes != null){
            projectHeadQuotes.setPaymentStatus(cProjectPrice.getPaymentStatus());
            cProjectHeadQuotesMapper.updateById(projectHeadQuotes);
        }

    }
}
